package com.china08.yxyapi.api.schwebsite.article;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.entity.mg.sws.SwsArticleForDailyAnalyze;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsArticleForDailyAnalyzeRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

/***
 * 学校网站:文章日点击量统计
 * 
 * @author ygl
 *
 */
@CrossOrigin
@RestController("SwsArticleForDailyCountController")
public class SwsArticleForDailyCountController extends YxyApiBaseController {

    @Autowired
    private SwsArticleForDailyAnalyzeRepository swsArticleForDailyAnalyzeRepository;

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/schwebsite/article/count", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String articleId = reqModel.articleId;
        String section1Id = reqModel.section1Id;
        String section2Id = reqModel.section2Id;
        RespModel model = new RespModel();
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String format = sFormat.format(date);
        String id = articleId + format;

        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int month = aCalendar.get(aCalendar.MONTH) + 1;
        int day = aCalendar.get(aCalendar.DATE);
        int yearForPv = aCalendar.get(aCalendar.YEAR);
        try {
            // 获取学校ID
            SwsArticle findById = swsArticleRepository.findById(articleId);

            // 判断今天是否浏览过
            SwsArticleForDailyAnalyze forDailyAnalyze = swsArticleForDailyAnalyzeRepository.findById(id);
            if (forDailyAnalyze != null) {
                forDailyAnalyze.setPv(forDailyAnalyze.getPv() + 1l);
                swsArticleForDailyAnalyzeRepository.save(forDailyAnalyze);
            } else {
                SwsArticleForDailyAnalyze sAnalyze = new SwsArticleForDailyAnalyze();
                sAnalyze.setId(id);
                sAnalyze.setArticleId(articleId);
                sAnalyze.setDateForPv(date);
                sAnalyze.setDayForPv(day);
                sAnalyze.setMonthForPv(month);
                sAnalyze.setSchoolId(findById.getOwnerSchoolId());
                sAnalyze.setPv(0l);
                sAnalyze.setYearForPv(yearForPv);
                sAnalyze.setSection1Id(section1Id);
                sAnalyze.setSection2Id(section2Id);
                swsArticleForDailyAnalyzeRepository.save(sAnalyze);
            }
            model.setMessage("修改文章浏览量成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            model.setMessage("修改文章浏览量失败");
        }

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 文章ID
        public String articleId;
        // 一级栏目ID
        public String section1Id;
        // 二级栏目ID
        public String section2Id;

    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
